home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / mtl100je.zip / EXAMPLE4.CPP < prev    next >
C/C++ Source or Header  |  1993-05-06  |  3KB  |  111 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE4.CPP: example for DOS multithreading library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      This example shows the use of a bounded buffer.  It starts
  17. //      two threads, each of which repeatedly gets a character from
  18. //      the buffer and either displays it or displays an asterisk.
  19. //      The main program is responsible for getting characters from
  20. //      the keyboard and putting them in the buffer.
  21. //
  22. //--------------------------------------------------------------------------
  23.  
  24. #include <stdio.h>
  25. #include <conio.h>
  26. #include "threads.h"
  27. #include "buffers.h"
  28.  
  29. BoundedBuffer<char> buffer (20);        // a buffer of 20 characters
  30.  
  31. class Example4a : public DOSThread
  32. {
  33.   public:
  34.     Example4a ()            { }
  35.     ~Example4a ();
  36.  
  37.   protected:
  38.     virtual void main ();
  39. };
  40.  
  41. void Example4a::main ()
  42. {
  43.     char c;
  44.     for (;;)
  45.     {   if (!buffer.get (c))
  46.             return;
  47.         putchar (c);
  48.     }
  49. }
  50.  
  51. Example4a::~Example4a ()
  52. {
  53.     wait ();
  54.     fputs ("\nEnd of thread A\n", stdout);
  55. }
  56.  
  57. class Example4b : public DOSThread
  58. {
  59.   public:
  60.     Example4b ()            { }
  61.     ~Example4b ();
  62.  
  63.   protected:
  64.     virtual void main ();
  65. };
  66.  
  67. void Example4b::main ()
  68. {
  69.     char c;
  70.     for (;;)
  71.     {   if (!buffer.get (c))
  72.             return;
  73.         putchar ('*');
  74.     }
  75. }
  76.  
  77. Example4b::~Example4b ()
  78. {
  79.     wait ();
  80.     fputs ("\nEnd of thread B\n", stdout);
  81. }
  82.  
  83. void main ()
  84. {
  85.     Example4a A;
  86.     Example4b B;
  87.  
  88.     if (A.run ())
  89.         puts ("Thread A started");
  90.     if (B.run ())
  91.         puts ("Thread B started");
  92.  
  93.     puts ("Press ESC to terminate");
  94.  
  95.     char c;
  96.     for (;;)
  97.     {   c = getch ();           // NB: if bioskey(0) is used instead of
  98.                                 // getch(), the program will crash if
  99.                                 // an upper memory manager is being used
  100.                                 // (e.g. QEMM or EMM386).  If you can
  101.                                 // figure out why, PLEASE let me know!
  102.         if (c == 0x1B)
  103.         {   buffer.close ();
  104.             puts ("\nTerminating...");
  105.             break;
  106.         }
  107.         else
  108.             buffer.put (c);
  109.     }
  110. }
  111.